package com.fly.r2dbc.basics.web;

import java.time.LocalDateTime;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@Api(tags = "flux接口")
@RestController
@RequestMapping("/flux")
public class WebFluxController
{
    @ApiOperation("mono1")
    @GetMapping("/mono1")
    public Mono<String> mono()
    {
        return Mono.just("Hello Mono -  Java North");
    }
    
    @ApiOperation("flux1")
    @GetMapping("/flux1")
    public Flux<String> flux()
    {
        return Flux.just("Hello Flux", "Hello Java North");
    }
    
    @ApiOperation("flux")
    @GetMapping(value = "/flux", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> flux1()
    {
        Flux<String> stringFlux = Flux.fromStream(IntStream.range(1, 6).mapToObj(i -> {
            try
            {
                TimeUnit.SECONDS.sleep(1);
            }
            catch (InterruptedException e)
            {
            }
            return "java north flux" + i + "date time: " + LocalDateTime.now();
        }));
        System.out.println("thread : " + Thread.currentThread().getName() + " ===  " + LocalDateTime.now() + "  ==========Flux function complete=========");
        return stringFlux;
    }
}
